# -*- encoding: utf-8 -*-
from shapely.wkt import loads as wkt_loads
import dsl
from . import FixtureTest


class RoadSortKeysRailways(FixtureTest):
    def test_rail(self):
        self.generate_fixtures(dsl.way(8920472, wkt_loads('LINESTRING (-122.39304153216 37.76442237615658, -122.392898520366 37.7639947225477, -122.392802041305 37.76360449179799, -122.392737632099 37.76319735713969)'), {u'tiger:name_base': u'Union Pacific Railroad', u'name': u'Peninsula', u'tiger:cfcc': u'B11', u'source': u'openstreetmap.org', u'electrified': u'no', u'operator': u'TransitAmerica Services', u'owner': u'Peninsula Joint Powers Board Railroad', u'tiger:county': u'San Francisco, CA', u'gauge': u'1435', u'usage': u'main', u'railway': u'rail', u'railway:traffic_mode': u'passenger'}))  # noqa

        self.assert_has_feature(
            16, 10487, 25333, "roads",
            {"kind": "rail", "kind_detail": "rail", "id": 8920472,
             "sort_rank": 382})

    def test_tram(self):
        self.generate_fixtures(dsl.way(1108824757, wkt_loads('POINT (-122.393708980416 37.79394395325019)'), {u'shelter': u'yes', u'wheelchair': u'yes', u'source': u'openstreetmap.org', u'route_ref': u'F', u'operator': u'San Francisco Municipal Railway', u'railway': u'tram_stop', u'ticker': u'no'}),dsl.way(1563088498, wkt_loads('POINT (-122.433132804301 37.76399777621059)'), {u'source': u'openstreetmap.org', u'railway': u'crossing'}),dsl.way(1563088506, wkt_loads('POINT (-122.433131726322 37.7640969846784)'), {u'source': u'openstreetmap.org', u'railway': u'level_crossing'}),dsl.way(1723563828, wkt_loads('POINT (-122.427598104172 37.7686328070861)'), {u'source': u'openstreetmap.org', u'railway': u'level_crossing'}),dsl.way(1723563856, wkt_loads('POINT (-122.428666829866 37.76778407917259)'), {u'source': u'openstreetmap.org', u'railway': u'crossing'}),dsl.way(1723563951, wkt_loads('POINT (-122.431210499424 37.76577207506551)'), {u'source': u'openstreetmap.org', u'railway': u'crossing'}),dsl.way(1723738870, wkt_loads('POINT (-122.420727878711 37.77407197587389)'), {u'source': u'openstreetmap.org', u'railway': u'level_crossing'}),dsl.way(1723738874, wkt_loads('POINT (-122.39480124197 37.79448160419319)'), {u'source': u'openstreetmap.org', u'railway': u'crossing'}),dsl.way(1723738886, wkt_loads('POINT (-122.393824144435 37.79387041104259)'), {u'source': u'openstreetmap.org', u'railway': u'crossing'}),dsl.way(1723739122, wkt_loads('POINT (-122.423299665538 37.77202818939159)'), {u'source': u'openstreetmap.org', u'railway': u'crossing'}),dsl.way(1723739307, wkt_loads('POINT (-122.420574176966 37.77418380983098)'), {u'source': u'openstreetmap.org', u'railway': u'crossing'}),dsl.way(2068535604, wkt_loads('POINT (-122.402134099802 37.78873221342861)'), {u'source': u'openstreetmap.org', u'railway': u'crossing'}),dsl.way(2068535629, wkt_loads('POINT (-122.401867659489 37.78894178050839)'), {u'source': u'openstreetmap.org', u'railway': u'crossing'}),dsl.way(4471556947, wkt_loads('POINT (-122.433099297141 37.76420038291137)'), {u'source': u'openstreetmap.org', u'railway': u'level_crossing'}),dsl.way(4471556948, wkt_loads('POINT (-122.433078725721 37.76423312092071)'), {u'source': u'openstreetmap.org', u'railway': u'level_crossing'}),dsl.way(4471556952, wkt_loads('POINT (-122.432833934806 37.7644485806896)'), {u'source': u'openstreetmap.org', u'railway': u'crossing'}),dsl.way(4472669436, wkt_loads('POINT (-122.423558829497 37.77182056257499)'), {u'source': u'openstreetmap.org', u'railway': u'crossing'}),dsl.way(4472669438, wkt_loads('POINT (-122.423467470833 37.77189270656919)'), {u'source': u'openstreetmap.org', u'railway': u'level_crossing'}),dsl.way(4472669440, wkt_loads('POINT (-122.423385993637 37.77195810475499)'), {u'source': u'openstreetmap.org', u'railway': u'level_crossing'}),dsl.way(4472669441, wkt_loads('POINT (-122.422948514093 37.77233082360449)'), {u'source': u'openstreetmap.org', u'railway': u'crossing'}),dsl.way(4472669443, wkt_loads('POINT (-122.422655842974 37.7725640115806)'), {u'source': u'openstreetmap.org', u'railway': u'level_crossing'}),dsl.way(4472669445, wkt_loads('POINT (-122.422024057834 37.77305119029881)'), {u'source': u'openstreetmap.org', u'railway': u'level_crossing'}),dsl.way(4477962359, wkt_loads('POINT (-122.393578006047 37.79404631593088)'), {u'source': u'openstreetmap.org', u'railway': u'crossing'}),dsl.way(4477962360, wkt_loads('POINT (-122.393552044736 37.79406661808239)'), {u'source': u'openstreetmap.org', u'railway': u'crossing'}),dsl.way(4493103508, wkt_loads('POINT (-122.419219158191 37.77522311198069)'), {u'source': u'openstreetmap.org', u'railway': u'level_crossing'}),dsl.way(4493103509, wkt_loads('POINT (-122.419333334064 37.77513286503429)'), {u'source': u'openstreetmap.org', u'railway': u'level_crossing'}),dsl.way(4493184768, wkt_loads('POINT (-122.412543867146 37.78050126612159)'), {u'source': u'openstreetmap.org', u'railway': u'level_crossing'}),dsl.way(4493184769, wkt_loads('POINT (-122.411986013355 37.7809421721093)'), {u'source': u'openstreetmap.org', u'railway': u'level_crossing'}),dsl.way(4493184770, wkt_loads('POINT (-122.411642138264 37.7812139563257)'), {u'source': u'openstreetmap.org', u'railway': u'level_crossing'}),dsl.way(4493184771, wkt_loads('POINT (-122.410402732667 37.78219359239488)'), {u'source': u'openstreetmap.org', u'railway': u'level_crossing'}),dsl.way(4493184772, wkt_loads('POINT (-122.410380634111 37.78221105788609)'), {u'source': u'openstreetmap.org', u'railway': u'level_crossing'}),dsl.way(4493184773, wkt_loads('POINT (-122.410373896746 37.78221631173219)'), {u'source': u'openstreetmap.org', u'railway': u'level_crossing'}),dsl.way(4495540991, wkt_loads('POINT (-122.400136965263 37.79030721758658)'), {u'source': u'openstreetmap.org', u'railway': u'level_crossing'}),dsl.way(4495540992, wkt_loads('POINT (-122.40038013921 37.7901154019639)'), {u'source': u'openstreetmap.org', u'railway': u'level_crossing'}),dsl.way(4495540994, wkt_loads('POINT (-122.402002945771 37.7888353642369)'), {u'source': u'openstreetmap.org', u'railway': u'level_crossing'}),dsl.way(4495540996, wkt_loads('POINT (-122.403025228564 37.78802605602398)'), {u'source': u'openstreetmap.org', u'railway': u'level_crossing'}),dsl.way(4495540998, wkt_loads('POINT (-122.403439441742 37.78769778672739)'), {u'source': u'openstreetmap.org', u'railway': u'level_crossing'}),dsl.way(4495540999, wkt_loads('POINT (-122.404627014547 37.7867567731256)'), {u'source': u'openstreetmap.org', u'railway': u'crossing'}),dsl.way(4496149787, wkt_loads('POINT (-122.398315541193 37.79174418614319)'), {u'source': u'openstreetmap.org', u'railway': u'level_crossing'}),dsl.way(4496150589, wkt_loads('POINT (-122.394956830177 37.79444355561498)'), {u'source': u'openstreetmap.org', u'railway': u'crossing'}),dsl.way(4496150591, wkt_loads('POINT (-122.394684371151 37.79441800058878)'), {u'source': u'openstreetmap.org', u'railway': u'crossing'}),dsl.way(4496150593, wkt_loads('POINT (-122.3964040161 37.7932856910975)'), {u'source': u'openstreetmap.org', u'railway': u'level_crossing'}),dsl.way(4496150594, wkt_loads('POINT (-122.396282474042 37.79337009492999)'), {u'source': u'openstreetmap.org', u'railway': u'level_crossing'}),dsl.way(4496150597, wkt_loads('POINT (-122.397427197208 37.792452082718)'), {u'source': u'openstreetmap.org', u'railway': u'level_crossing'}),dsl.way(4496150598, wkt_loads('POINT (-122.397409410566 37.79246571242899)'), {u'source': u'openstreetmap.org', u'railway': u'level_crossing'}),dsl.way(4540191132, wkt_loads('POINT (-122.431045478907 37.76590188801538)'), {u'source': u'openstreetmap.org', u'railway': u'level_crossing'}),dsl.way(4540191133, wkt_loads('POINT (-122.430902826439 37.76601416041039)'), {u'source': u'openstreetmap.org', u'railway': u'level_crossing'}),dsl.way(4540191134, wkt_loads('POINT (-122.430332036908 37.76646530766418)'), {u'source': u'openstreetmap.org', u'railway': u'level_crossing'}),dsl.way(4540191137, wkt_loads('POINT (-122.42909847036 37.76744251172631)'), {u'source': u'openstreetmap.org', u'railway': u'crossing'}),dsl.way(4540191139, wkt_loads('POINT (-122.42896920279 37.76754469807781)'), {u'source': u'openstreetmap.org', u'railway': u'level_crossing'}),dsl.way(4540191140, wkt_loads('POINT (-122.426897418251 37.7691834950623)'), {u'source': u'openstreetmap.org', u'railway': u'level_crossing'}),dsl.way(4540191142, wkt_loads('POINT (-122.427180207902 37.7689613030764)'), {u'source': u'openstreetmap.org', u'railway': u'crossing'}),dsl.way(4557135183, wkt_loads('POINT (-122.425057309223 37.77063621227771)'), {u'source': u'openstreetmap.org', u'railway': u'crossing'}),dsl.way(4557135186, wkt_loads('POINT (-122.424699600077 37.77091889872658)'), {u'source': u'openstreetmap.org', u'railway': u'crossing'}),dsl.way(4603460300, wkt_loads('POINT (-122.426325730404 37.76963717934171)'), {u'source': u'openstreetmap.org', u'railway': u'level_crossing'}),dsl.way(4603460308, wkt_loads('POINT (-122.426220627516 37.76972260432189)'), {u'crossing': u'traffic_signals', u'source': u'openstreetmap.org', u'railway': u'crossing', u'bicycle': u'yes'}),dsl.way(4700256953, wkt_loads('POINT (-122.405947268521 37.78571387041999)'), {u'source': u'openstreetmap.org', u'railway': u'level_crossing'}),dsl.way(4700256954, wkt_loads('POINT (-122.407372894876 37.78458866909681)'), {u'source': u'openstreetmap.org', u'railway': u'crossing'}),dsl.way(4700256955, wkt_loads('POINT (-122.408086965696 37.78402467758559)'), {u'source': u'openstreetmap.org', u'railway': u'level_crossing'}),dsl.way(4700256957, wkt_loads('POINT (-122.408776692171 37.78347885692918)'), {u'source': u'openstreetmap.org', u'railway': u'level_crossing'}),dsl.way(4700256958, wkt_loads('POINT (-122.409102780619 37.7832208544512)'), {u'source': u'openstreetmap.org', u'railway': u'level_crossing'}),dsl.way(95623728, wkt_loads('LINESTRING (-122.393410021089 37.7941774989649, -122.393552044736 37.79406661808239, -122.393578006047 37.79404631593088, -122.393708980416 37.79394395325019, -122.393765394616 37.79390001249979, -122.393824144435 37.79387041104259, -122.393876875542 37.79386125375478, -122.393934996541 37.79386735861347, -122.39397488174 37.79388240779771, -122.394006232943 37.79390292295418, -122.394046926626 37.79393500893161, -122.394684371151 37.79441800058878, -122.394697127228 37.79442857753129, -122.394726771633 37.79444966042549, -122.394759021151 37.79446875570159, -122.39480124197 37.79448160419319, -122.394835288119 37.79448259799909, -122.394872837698 37.79448046841497, -122.39491173475 37.79447095627219, -122.394943984268 37.79445285480291, -122.394956830177 37.79444355561498, -122.394976143956 37.79442957133787, -122.396175664354 37.79345826098348, -122.396282474042 37.79337009492999, -122.3964040161 37.7932856910975, -122.396565533188 37.79312937693258, -122.396636500095 37.79306988943868, -122.397230645824 37.79260151248579, -122.397409410566 37.79246571242899, -122.397427197208 37.792452082718, -122.39750957272 37.79238947120049, -122.397921180783 37.79206526763029, -122.398187980422 37.79184477715489, -122.398315541193 37.79174418614319, -122.400136965263 37.79030721758658, -122.40038013921 37.7901154019639, -122.401867659489 37.78894178050839, -122.402002945771 37.7888353642369, -122.402134099802 37.78873221342861, -122.403025228564 37.78802605602398, -122.403317899684 37.78779412369417, -122.403439441742 37.78769778672739, -122.404627014547 37.7867567731256, -122.40488339373 37.78655358928719, -122.405947268521 37.78571387041999, -122.407372894876 37.78458866909681, -122.407948265816 37.78413450859618, -122.408086965696 37.78402467758559, -122.408776692171 37.78347885692918, -122.408947731401 37.78334346608789, -122.409102780619 37.7832208544512, -122.410373896746 37.78221631173219, -122.410380634111 37.78221105788609, -122.410402732667 37.78219359239488, -122.411642138264 37.7812139563257, -122.411986013355 37.7809421721093, -122.412543867146 37.78050126612159, -122.414703327258 37.77879455885978, -122.417400788393 37.77666378199078, -122.418227687612 37.77600671498148, -122.418493588936 37.77579647218629, -122.419219158191 37.77522311198069, -122.419333334064 37.77513286503429, -122.420202184607 37.77444617518047, -122.420289411021 37.7743763058522, -122.420378074739 37.77431261393759, -122.420483806448 37.77424267347747, -122.420574176966 37.77418380983098, -122.420647389661 37.77413240173457, -122.420727878711 37.77407197587389, -122.421881944356 37.77315841036979, -122.421985430277 37.7730790959349, -122.422024057834 37.77305119029881, -122.422185664754 37.7729345971709, -122.422306308497 37.77284257225799, -122.422655842974 37.7725640115806, -122.422948514093 37.77233082360449, -122.422992890868 37.7722964560087, -122.423085686837 37.7722185607996, -122.423201389846 37.77211020324359, -122.423299665538 37.77202818939159, -122.423385993637 37.77195810475499, -122.423426238161 37.77192537016538, -122.423467470833 37.77189270656919, -122.423558829497 37.77182056257499, -122.424699600077 37.77091889872658, -122.425036378477 37.77065268637318, -122.425057309223 37.77063621227771, -122.425906666324 37.7699670200543, -122.426194486541 37.76974376527428, -122.426220627516 37.76972260432189, -122.426325730404 37.76963717934171, -122.426341181427 37.76962461057069, -122.426897418251 37.7691834950623, -122.427180207902 37.7689613030764, -122.427598104172 37.7686328070861, -122.427641402969 37.7685997869804, -122.427924012957 37.76837546290438, -122.428354036484 37.76803056064709, -122.428666829866 37.76778407917259, -122.428950428001 37.76755918452618, -122.42896920279 37.76754469807781, -122.428986630107 37.76753120579489, -122.42909847036 37.76744251172631, -122.430332036908 37.76646530766418, -122.430712563262 37.76616378565848, -122.430902826439 37.76601416041039, -122.431045478907 37.76590188801538, -122.431210499424 37.76577207506551, -122.43225290448 37.76494916360308, -122.432360073493 37.7648552113582, -122.432498144553 37.76472056757021, -122.432596510076 37.76463740928551, -122.432833934806 37.7644485806896, -122.432985570426 37.76432785512178, -122.433048991485 37.76426877053679, -122.433078725721 37.76423312092071, -122.433089146178 37.76422069326158, -122.433099297141 37.76420038291137, -122.433114837995 37.76416942026888, -122.433131726322 37.7640969846784, -122.433134241605 37.76405018556808, -122.433132804301 37.76399777621059, -122.433024916635 37.762895111646, -122.43301287921 37.7627643702028, -122.433014316515 37.7627285068115, -122.433026443771 37.76269598118628)'), {u'source': u'openstreetmap.org', u'railway': u'tram', u'name': u'Muni F', u'oneway': u'yes'}))  # noqa

        self.assert_has_feature(
            16, 10486, 25326, "roads",
            {"kind": "rail", "kind_detail": "tram", "id": 95623728,
             "sort_rank": 382})

    def test_light_rail(self):
        self.generate_fixtures(dsl.way(1718992959, wkt_loads('POINT (-122.438462688545 37.72991545355039)'), {u'source': u'openstreetmap.org', u'railway': u'level_crossing'}),dsl.way(160279679, wkt_loads('LINESTRING (-122.438552070915 37.72986067542419, -122.438462688545 37.72991545355039, -122.438370341733 37.7299662529438, -122.438270179579 37.73001499190951, -122.438149266342 37.7300662885706, -122.438038863393 37.73010550704638, -122.437906002563 37.73014365978288, -122.437775836678 37.7301752050474, -122.437444717665 37.73024952109041, -122.436208815497 37.7305180813121, -122.436088980238 37.7305490580369, -122.435969414473 37.73058820520968, -122.435851914834 37.73064440374629, -122.435736301657 37.73071246715069, -122.435653117662 37.73078081468179, -122.4355848457 37.73085122251937, -122.435517382222 37.7309300138548, -122.435446594978 37.73103168226189, -122.435389012968 37.73113157435541)'), {u'name': u'Muni J', u'electrified': u'contact_line', u'source': u'openstreetmap.org', u'frequency': u'0', u'gauge': u'1435', u'voltage': u'600', u'oneway': u'yes', u'railway': u'light_rail'}))  # noqa

        self.assert_has_feature(
            16, 10478, 25341, "roads",
            {"kind": "rail", "kind_detail": "light_rail", "id": 160279679,
             "sort_rank": 382})

    def test_narrow_gauge(self):
        self.generate_fixtures(dsl.way(105574666, wkt_loads('LINESTRING (-74.44287932819699 40.13493861208138, -74.44282030888279 40.1349980878628)'), {u'source': u'openstreetmap.org', u'railway': u'narrow_gauge'}))  # noqa

        self.assert_has_feature(
            16, 19216, 24778, "roads",
            {"kind": "rail", "kind_detail": "narrow_gauge", "id": 105574666,
             "sort_rank": 382})

    def test_monorail(self):
        self.generate_fixtures(dsl.way(296530703, wkt_loads('LINESTRING (-82.68938578864038 41.48358272065969, -82.6895748840077 41.48360889914459)'), {u'source': u'openstreetmap.org', u'railway': u'monorail'}))  # noqa

        self.assert_has_feature(
            16, 17714, 24454, "roads",
            {"kind": "rail", "kind_detail": "monorail", "id": 296530703,
             "sort_rank": 382})

    def test_spur(self):
        # spurs, sidings, etc...
        self.generate_fixtures(dsl.way(1487041071, wkt_loads('POINT (-106.648475890418 35.07718587778591)'), {u'source': u'openstreetmap.org', u'railway': u'switch'}),dsl.way(135403703, wkt_loads('LINESTRING (-106.649071024293 35.07403947074057, -106.648987570803 35.07451947511569, -106.648884803535 35.07504616069249, -106.648804943306 35.07547976946611, -106.648697414967 35.076037399607, -106.64857946617 35.07661737502089, -106.648547396314 35.07681278221138, -106.648503289034 35.0770306849374, -106.648475890418 35.07718587778591)'), {u'source': u'openstreetmap.org', u'railway': u'rail', u'service': u'spur'}))  # noqa

        self.assert_has_feature(
            16, 13353, 25941, "roads",
            {"kind": "rail", "kind_detail": "rail", "service": "spur",
             "id": 135403703, "sort_rank": 361})

    def test_siding(self):
        self.generate_fixtures(dsl.way(148018328, wkt_loads('LINESTRING (-122.400045516767 37.7719329679999, -122.400096900401 37.77182986460968, -122.40011801081 37.77177696370978, -122.400128970257 37.77172470184399, -122.400141905997 37.77164197748359, -122.400144600943 37.77156656687009, -122.400137324589 37.77147297807339, -122.400106961532 37.77129588331901, -122.400071208584 37.77116970127259, -122.400028987766 37.77105218206551, -122.399974819354 37.7709541900762, -122.399906008403 37.7708511563301, -122.399825160028 37.77074819344938, -122.399731645407 37.7706502010573, -122.399661846309 37.7705863639146, -122.399511288667 37.77044342248209, -122.399398370436 37.77033236397018, -122.39916759324 37.77011507509489)'), {u'service': u'siding', u'electrified': u'no', u'source': u'openstreetmap.org', u'gauge': u'1435', u'railway': u'rail', u'railway:traffic_mode': u'passenger'}))  # noqa

        self.assert_has_feature(
            16, 10485, 25331, "roads",
            {"kind": "rail", "kind_detail": "rail", "service": "siding",
             "id": 148018328, "sort_rank": 361})

    def test_yard(self):
        self.generate_fixtures(dsl.way(1344032371, wkt_loads('POINT (-122.401192216227 37.7716789726811)'), {u'source': u'openstreetmap.org', u'railway': u'buffer_stop'}),dsl.way(119709585, wkt_loads('LINESTRING (-122.399873579222 37.772668676103, -122.400049469354 37.77256117129298, -122.400127802447 37.77251331243039, -122.400226886623 37.7724467076115, -122.400287522904 37.7724011919172, -122.401192216227 37.7716789726811)'), {u'service': u'yard', u'electrified': u'no', u'source': u'openstreetmap.org', u'gauge': u'1435', u'railway': u'rail', u'railway:traffic_mode': u'passenger'}))  # noqa

        self.assert_has_feature(
            16, 10485, 25331, "roads",
            {"kind": "rail", "kind_detail": "rail", "service": "yard",
             "id": 119709585, "sort_rank": 359})

    def test_crossover(self):
        self.generate_fixtures(dsl.way(119695339, wkt_loads('LINESTRING (-122.401795524772 37.70255026317898, -122.401807202871 37.7028924144261, -122.40181537754 37.70309056860139, -122.401826516649 37.7032822545431, -122.401834691318 37.70343989578041, -122.401847088069 37.70374565343717)'), {u'name_2': u'Union Pacific Railroad', u'name_1': u'Union Pacific Railroad', u'name': u'Caltrain', u'service': u'crossover', u'electrified': u'no', u'source': u'openstreetmap.org', u'gauge': u'1435', u'railway': u'rail'}))  # noqa

        self.assert_has_feature(
            16, 10485, 25347, "roads",
            {"kind": "rail", "kind_detail": "rail", "service": "crossover",
             "id": 119695339, "sort_rank": 357})
